package com.chinaunicom.emergency.action;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author liliang
 * @date 2018/1/159-12-13
 */
@RestController
public class CommonAction extends BaseAction {

    @PersistenceContext
    EntityManager em;

    @GetMapping("/get/{className}/{id}")
    public Object getOne(@PathVariable("className") String className, @PathVariable(name = "id") String id) {
        Query query = em.createQuery(" from " + className + " where id = ?");
        query.setParameter(1, Integer.valueOf(id));
        List list = query.getResultList();
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return error("未查询数据，id:" + id);
        }
    }
}
